<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function banner(){
        $type = $this->request->post('type');
        $list = Db::name("banner")->where(['type'=>$type])->select();
        foreach ($list as $k=>$v){
            $list[$k]['image'] = cdnurl($v['image'],true);
        }
        $this->success("首页 banner",$list);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 首页课程分类
     */
    public function indexCourseLabel(){
        $label_list = Db::name("course_label")->field("id,image,name,banner,yy_time,phone")->select();
        foreach ($label_list as $k=>$v){
            $label_list[$k]['image'] = cdnurl($v['image'],true);
            $label_list[$k]['banner'] = cdnurl($v['banner'],true);
            $course_category_list = Db::name("course_category")->where(['course_label_id'=>$v['id']])->select();
            foreach ($course_category_list as $k1=>$v1){
                $course_category_list[$k1]['image'] = cdnurl($v1['image'],true);

            }
            $label_list[$k]['course_category_list'] = $course_category_list;
        }
        $this->success('首页课程分类',$label_list);
    }
    public function coursehx(){
        $list = Db::name("course_order_pre")->where(['status'=>0,'day'=>['<',time()]])->select();
        foreach ($list as $k=>$v){
            $timetable_data = Db::name("timetable")->where(['id'=>$v['course_time_ids']])->find();
            $c_time_arr = explode('-',$timetable_data['c_time']);
            $str_time = strtotime(date('Y-m-d',$timetable_data['time']).$c_time_arr[0]);
            $pre_re = Db::name("config")->where(['name'=>'pre_re'])->value('value');
            $str_time = $str_time + $pre_re*60;
            if(time() > $str_time){//开课超过 30分钟自动审核
                Db::name("course_order_pre")->where(['id'=>$v['id']])->update(['status'=>1]);
            }
        }
        echo "成功";
    }
    public function exre(){
        $ex_list = Db::name("user")->where(['is_examine'=>1])->select();
        foreach ($ex_list as $k=>$v){
            $m_data = Db::name("merchant")->where(['examine_user_ids_str'=>['like',','.$v['id'].',']])->find();
            if(!$m_data){
                Db::name("user")->where(['id'=>$v['id']])->update(['is_examine'=>0]);
            }
        }
        $mer_list = Db::name("user")->where(['is_merchant'=>1])->select();
        foreach ($mer_list as $k=>$v){
            $mr_data = Db::name("merchant")->where(['user_id'=>$v['id']])->find();
            if(!$mr_data){
                Db::name("user")->where(['id'=>$v['id']])->update(['is_merchant'=>0]);
            }
        }
    }
}
